package com.storage.system.service.impl;

import com.storage.common.core.domain.AjaxResult;
import com.storage.common.core.domain.ServiceResponse;
import com.storage.common.core.page.TableDataInfo;
import com.storage.common.core.text.Convert;
import com.storage.common.enums.RoleType;
import com.storage.system.domain.StorageCategory;
import com.storage.system.domain.StorageUser;
import com.storage.system.mapper.StorageCategoryMapper;
import com.storage.system.service.IStorageCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类 服务层实现
 *
 * @author storage
 * @date 2019-08-18
 */
@Service
public class StorageCategoryServiceImpl implements IStorageCategoryService {
    @Autowired
    private StorageCategoryMapper storageCategoryMapper;

    /**
     * 查询分类信息
     *
     * @param id 分类ID
     * @return 分类信息
     */
    @Override
    public StorageCategory selectStorageCategoryById(Long id) {
        return storageCategoryMapper.selectStorageCategoryById(id);
    }

    /**
     * 查询分类列表
     *
     * @param storageCategory 分类信息
     * @return 分类集合
     */
    @Override
    public ServiceResponse<TableDataInfo> selectStorageCategoryList(StorageUser currentUser, StorageCategory storageCategory, Integer pageNum, Integer pageSize) {
        Long canOperationId = RoleType.getOperationUserId(currentUser.getRole(), currentUser.getId(), currentUser.getShopId());
        storageCategory.setCreateUserId(canOperationId);
        storageCategory.setOwnerType(RoleType.getOwnerType(currentUser.getRole()));


        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

        Integer start = (pageNum - 1) * pageSize;
        // StorageCategory
        List<StorageUser> users = storageCategoryMapper.queryPageList(storageCategory, start, pageSize);
        // 查询totalCount
        Long totalCount = storageCategoryMapper.queryPageCount(storageCategory);
        if (users == null) {
            return null;
        }

        TableDataInfo pageList = new TableDataInfo(users, totalCount, pageNum, pageSize);
        return new ServiceResponse<>(true, pageList, null);
    }

    /**
     * 新增分类
     *
     * @param storageCategory 分类信息
     * @return 结果
     */
    @Override
    public AjaxResult insertStorageCategory(StorageUser currentUser, StorageCategory storageCategory) {
        Long canOperationId = RoleType.getOperationUserId(currentUser.getRole(), currentUser.getId(), currentUser.getShopId());
        Integer ownerType = RoleType.getOwnerType(currentUser.getRole());
        if (canOperationId == null) {
            return AjaxResult.error("当前用不具备该操作权限");
        } else if (StringUtils.isBlank(storageCategory.getCategoryName())) {
            return AjaxResult.error("分类名称不能为空");
        } else if (storageCategoryMapper.selectCategoryCountByName(storageCategory.getCategoryName(), canOperationId, ownerType) > 0) {
            return AjaxResult.error("已存在相同的分类名称");
        }

        storageCategory.setOwnerType(RoleType.getOwnerType(currentUser.getRole()));
        storageCategoryMapper.insertStorageCategory(storageCategory);
        return AjaxResult.success(storageCategoryMapper.selectStorageCategoryById(storageCategory.getId()));
    }

    /**
     * 修改分类
     *
     * @param storageCategory 分类信息
     * @return 结果
     */
    @Override
    public AjaxResult updateStorageCategory(StorageUser currentUser, StorageCategory storageCategory) {
        Long canOperationId = RoleType.getOperationUserId(currentUser.getRole(), currentUser.getId(), currentUser.getShopId());
        if (canOperationId == null) {
            return AjaxResult.error("当前用不具备该操作权限");
        } else if (StringUtils.isBlank(storageCategory.getCategoryName())) {
            return AjaxResult.error("分类名称不能为空");
        }

        StorageCategory categoryCondition = new StorageCategory();
        categoryCondition.setCategoryName(storageCategory.getCategoryName());
        List<StorageCategory> exists = storageCategoryMapper.selectStorageCategoryList(categoryCondition);
        if (exists != null && exists.size() > 0) {
            return AjaxResult.error("已经存在相同名称的分类");
        }
        storageCategory.setOwnerType(RoleType.getOwnerType(currentUser.getRole()));
        storageCategoryMapper.updateStorageCategory(storageCategory);
        return AjaxResult.success(storageCategory);
    }

    /**
     * 删除分类对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStorageCategoryByIds(String ids) {
        return storageCategoryMapper.deleteStorageCategoryByIds(Convert.toStrArray(ids));
    }

    @Override
    public ServiceResponse<List<StorageCategory>> selectStorageCategorySelectList(StorageUser storageUser, StorageCategory storageCategory) {
        List<StorageCategory> categories = storageCategoryMapper.selectStorageCategoryList(storageCategory);
        return new ServiceResponse<>(true, categories, null);
    }

}
